package vn.edu.saigontech.ncms.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.util.I18n;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {

	public static final String ADD = "add";
	public static final String INFO = "info";
	public static final String EDIT = "edit";
	public static final String RESULT = "result";
	public static final String REDIRECT = "redirect";
	public static final String LOGIN = "login";
	public static final String EDUCATION = "education";
	
	// TO LOG ONLY
	public static final String DELETE = "delete";
	public static final String REPORT = "report";
	public static final String SEARCH = "search";
	
	protected String message;
	protected int version;

	protected List<BaseLogging> loggings;
	
	public void addLoggingDetail(String action, String key, Object...params) {
		if(loggings == null)
			loggings = new ArrayList<BaseLogging>();
		
		BaseLogging logging = new BaseLogging();
		User user = (User) SESSION().getAttribute("user");		
		logging.setInteractedAction(action);
		logging.setDescription(this.i18n(key, params));
		logging.setActor(user);
		logging.setInteractedDate(new Date());
		
		this.loggings.add(logging);
	}
	
	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setValueMessage(String key, Object... params) {
		this.setMessage(this.i18n(key, params));
	}

	public void setKeyMessage(String key, String... paramKeys) {
		Object[] values = new Object[paramKeys.length];
		for (int i = 0; i < paramKeys.length; i++)
			values[i] = this.i18n(paramKeys[i]);
		this.setValueMessage(key, values);
	}

	public I18n i18n() {
		return (I18n) SESSION().getAttribute("i18n");
	}
	
	public String i18n(String key, Object... params) {
		return this.i18n().getMessage(key, params);
	}

	public HttpServletRequest REQUEST() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse RESPONSE() {
		return ServletActionContext.getResponse();
	}

	public HttpSession SESSION() {
		return ServletActionContext.getRequest().getSession();
	}

	public ServletContext APPLICATION() {
		return ServletActionContext.getServletContext();
	}

	public ValueStack STACK() {
		return ActionContext.getContext().getValueStack();
	}

	public User USER() {
		return (User) SESSION().getAttribute("user");
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public List<BaseLogging> getLoggings() {
		return loggings;
	}
}